﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MedicWave.Visualization3d;
using System.IO;

namespace TestForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            
        }

        void Form1_Load(object sender, EventArgs e)
        {
            PlotData data = ReadFileData("wt15.txt");
            data.PlotName = "Plot1";
            widgetPlot3D1.SetData(data);

            //sample usage of SetPlotSettings:

            //PlotSettings settings = widgetPlot3D1.GetCurrentSettings();
            //settings.PlotType = PlotType.Points;
            //settings.ShowGrid = true;
            //settings.FilteringType = DataFilteringType.Plot;
            //settings.FilteringAmount = 5;
            //settings.LockPlotType = true;
            //widgetPlot3D1.SetPlotSettings(settings);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            /*Random rnd = new Random();
            // Creating Sample Data
            ArrayList arlElements = new ArrayList();
            int RowCount = rnd.Next(5, 25);
            for (int i = 0; i < RowCount; i++)
            {
                float RT = (float)rnd.NextDouble() * 5000;
                int MZ = rnd.Next(0, 300);
                int iPickCount = 0;
                for (int j = 0; j < MZ;j++ )
                {
                    Plot3DControl.PairVector tmpPair = new Plot3DControl.PairVector();
                    tmpPair.m_fRt = RT;
                    tmpPair.m_cPD.m_fMZ = (float)rnd.NextDouble() * 500;
                    float Intens = (float)rnd.NextDouble() * 200;
                    if(iPickCount<6 && Intens > 170)
                    {
                        tmpPair.m_cPD.m_fIntensity = Intens;
                        iPickCount++;
                    }
                    else
                    {
                        tmpPair.m_cPD.m_fIntensity = Intens/100.0f;
                    }
                    
                    arlElements.Add(tmpPair);
                }                

            }

            arlElements.Sort();

            widgetPlot3D1.SetData(arlElements);*/
            PlotData data = ReadFileData("sampleLCMS.txt");
            
            //widgetPlot3D1.SetData(data);
            PlotSettings settings = widgetPlot3D1.GetCurrentSettings();

            settings.PlotType = PlotType.Lines;
            widgetPlot3D1.SetPlotSettings(settings);
        }

        private PlotData ReadFileData(string sFileName)
        {
            StreamReader file;
            ArrayList arrList= new ArrayList();
            try
            {
                file = new StreamReader(sFileName);
                string line;
                Scan tmpPV=null;//= new Scan();                
                while(!file.EndOfStream)
                {
                    line = file.ReadLine();
                    if (line.Contains("RT"))
                    {
                        line = file.ReadLine();
                        line=line.Replace('.', ',');
                        tmpPV = new Scan((float)System.Convert.ToDouble(line));                   
                    }
                    else if (line.Contains("MZ"))
                    {
                        line = file.ReadLine();
                        line=line.Replace(".", ",");
                        string[] sArr = line.Split(' ');
                        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//tmpPV.m_PD = new PairData[sArr.Length];
                        tmpPV.ScanData = new DataPoint[sArr.Length];
                        //tmpPV.m_PD = new PairData[1000];
                        for (int i = 0; i < sArr.Length; i++ )
                        {
                            //if (i > 999)
                            //    break;
                            DataPoint tmpPD = new DataPoint();
                            tmpPD.MZ = (float)System.Convert.ToDouble(sArr[i]);
                            tmpPV.ScanData[i] = tmpPD;                            
                        }
                    }
                    else if (line.Contains("INTENSITY"))
                    {
                        line = file.ReadLine();
                        string[] sArr = line.Split(' ');
                        for (int i = 0; i < sArr.Length; i++)
                        {
                            //if (i > 999)
                            //    break;
                            tmpPV.ScanData[i].Intensity = (float)System.Convert.ToDouble(sArr[i]);
                        }
                        arrList.Add(tmpPV);
                    }                    
                }                
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
            

            return new PlotData((Scan[])arrList.ToArray(typeof(Scan)));
        }

      
        private void btnPIc_Click(object sender, EventArgs e)
        {
            ScatterplotSettingsForm form = new ScatterplotSettingsForm();
            if( form.ShowDialog() == DialogResult.OK)
            {

            }
        }
    }
}
